package concurrent;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class EventChecker implements Runnable {

	private IntGenerator generator;

	private final int id;

	public EventChecker(IntGenerator generator,int id){
		this.generator = generator;
		this.id = id;
	}

	@Override
	public void run() {
		while(!generator.isCanceled()){
			int val = generator.next();
			System.out.println(id+ "----" + val);
			if(val % 2 != 0 ){
				System.out.println(id+ "  " + val + " is not event!!!");
				generator.cancel(id);
			}
		}
	}
	
	public static void Test(IntGenerator generator,int count){
		ExecutorService service = Executors.newCachedThreadPool();
		for(int i = 0; i < count; ++i){
			service.execute(new EventChecker(generator,i));
		}
		service.shutdown();
	}
	
	public static void test(IntGenerator generator){
		Test(generator,10);
	}

}
